package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.conf.*;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.MsgAnnoucesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
* @author pc
* @description 针对表【msg_annouces】的数据库操作Service实现
* @createDate 2024-03-10 13:35:34
*/
@Service
public class MsgAnnoucesServiceImpl extends MPJBaseServiceImpl<MsgAnnoucesMapper, MsgAnnouces>
    implements MsgAnnoucesService{

    @Resource
    private MsgAnnoucetoService msgAnnoucetoService;

    @Resource
    private MsgAnnoucesReadService msgAnnoucesReadService;

    @Override
    public IPage<AnnoResVo> msgAnnoViewList(String $usrid,AnnoReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                AnnoResVo.class,
                new MPJLambdaWrapper<MsgAnnouces>()
                        .selectAs("(select name from sys_user u where u.usrid=t.relusr)",AnnoResVo::getRelusrname)
                        .selectAs("(select name from sys_user u where u.usrid=t.mdusr)",AnnoResVo::getMdusrname)
                        .selectAs(String.format("(select count(1) from msg_annouces_read anr where anr.annouceid=t.annouceid and anr.readusr='%s')",$usrid),AnnoResVo::getStatus)
                        .select(MsgAnnouces::getAnnouceid,MsgAnnouces::getTitle,MsgAnnouces::getContent,MsgAnnouces::getReldt,
                                MsgAnnouces::getRelusr,MsgAnnouces::getMddt,MsgAnnouces::getMdusr)
                        .eq(MsgAnnouces::getStatus,"1")
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), MsgAnnouces::getReldt,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .exists(String.format("select 1 from msg_annouceto an where an.annouceid=t.annouceid and an.grpid in(select mgr.grpid from sys_grpmgr mgr where mgr.grpmanagerid='%s')",$usrid))
                        .notExists(JPubUtils.IsEqual(paras.getStatus(),"0"),String.format("select 1 from msg_annouces_read anr where anr.annouceid=t.annouceid and anr.readusr='%s'",$usrid))
                        .exists(JPubUtils.IsEqual(paras.getStatus(),"1"),String.format("select 1 from msg_annouces_read anr where anr.annouceid=t.annouceid and anr.readusr='%s'",$usrid))
                        .orderByDesc("reldt")
        );
    }

    @Override
    public IPage<AnnoResVo> msgAnnoList(String $usrid,AnnoReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                AnnoResVo.class,
                new MPJLambdaWrapper<MsgAnnouces>()
                        .selectAs("(select name from sys_user u where u.usrid=t.relusr)",AnnoResVo::getRelusrname)
                        .selectAs("(select name from sys_user u where u.usrid=t.mdusr)",AnnoResVo::getMdusrname)
                        .selectAll(MsgAnnouces.class)
                        .eq(JPubUtils.IsNotEmpty(paras.getStatus()),MsgAnnouces::getStatus,paras.getStatus())
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), MsgAnnouces::getReldt,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .orderByDesc(MsgAnnouces::getCrdt)
        );
    }

    @Override
    public AnnoResVo msgAnnoInfo(Integer annouceid)
    {
        List<MsgAnnouceto>  annoucetoList=msgAnnoucetoService.getBaseMapper().selectList(
                new QueryWrapper<MsgAnnouceto>()
                        .eq("annouceid",annouceid)
        );
        AnnoResVo annoResVo=selectJoinOne(AnnoResVo.class,
                new MPJQueryWrapper<MsgAnnouces>()
                        .eq("annouceid",annouceid)
                        .selectAll(MsgAnnouces.class)
        );
        List<String> groups=new LinkedList<>();
        for(MsgAnnouceto annouceto:annoucetoList){
            groups.add(annouceto.getGrpid());
        }
        annoResVo.setGroups(groups);

        return annoResVo;
    }

    @Override
    public CmdResVo updateAnno(String $usrid,AnnoEditReqVo paras)
    {
        int ret=0;
        MsgAnnouces annouces=new MsgAnnouces();
        BeanUtils.copyProperties(paras,annouces);
        annouces.setMddt(new Date());
        annouces.setMdusr($usrid);
        if(JPubUtils.IsEqual(paras.getStatus(),"1")){
            annouces.setReldt(new Date());
            annouces.setRelusr($usrid);
            ret=baseMapper.updateById(annouces);

            msgAnnoucetoService.remove(new QueryWrapper<MsgAnnouceto>().eq("annouceid",paras.getAnnouceid()));

            if(ret>0&&paras.getGroups()!=null) {
                List<MsgAnnouceto> anlist=new ArrayList<>();
                for (String grp : paras.getGroups()) {
                    MsgAnnouceto annouceto=new MsgAnnouceto();
                    annouceto.setAnnouceid(paras.getAnnouceid());
                    annouceto.setGrpid(grp);
                    anlist.add(annouceto);
                }
                msgAnnoucetoService.saveBatch(anlist);
            }

            //修改后显示未读
            msgAnnoucesReadService.remove(new QueryWrapper<MsgAnnoucesRead>()
                    .eq("annouceid",paras.getAnnouceid())
            );
        }
        else {
            ret = baseMapper.updateById(annouces);
        }

        return new CmdResVo(ret,annouces.getAnnouceid().toString());
    }

    @Override
    public CmdResVo insertAnno(String $usrid,AnnoEditReqVo paras)
    {
        int ret=0;
        MsgAnnouces annouces=new MsgAnnouces();
        BeanUtils.copyProperties(paras,annouces);
        annouces.setCrdt(new Date());
        annouces.setCrusr($usrid);
        annouces.setMddt(new Date());
        annouces.setMdusr($usrid);
        if(JPubUtils.IsEqual(paras.getStatus(),"1")){
            annouces.setReldt(new Date());
            annouces.setRelusr($usrid);
            ret=baseMapper.insert(annouces);

            if(ret>0&&paras.getGroups()!=null) {
                List<MsgAnnouceto> anlist=new ArrayList<>();
                for (String grp : paras.getGroups()) {
                    MsgAnnouceto annouceto=new MsgAnnouceto();
                    annouceto.setAnnouceid(annouces.getAnnouceid());
                    annouceto.setGrpid(grp);
                    anlist.add(annouceto);
                }
                msgAnnoucetoService.saveBatch(anlist);
            }
        }
        else{
            ret=baseMapper.insert(annouces);
        }

        return new CmdResVo(ret,annouces.getAnnouceid().toString());
    }

    @Override
    public CmdResVo deleteAnno(Integer id)
    {
        msgAnnoucetoService.remove(new QueryWrapper<MsgAnnouceto>().eq("annouceid",id));
        int ret= baseMapper.deleteById(id);
        return new CmdResVo(ret,id.toString());
    }

    @Override
    public CmdResVo getAnnoNoReadCount(String $usrid)
    {
        Long num=selectJoinCount(new MPJQueryWrapper<MsgAnnouces>()
                .eq("status","1")
                .exists(String.format("select 1 from msg_annouceto an where an.ANNOUCEID=t.ANNOUCEID and an.GRPID in(select mgr.GRPID from sys_grpmgr mgr where mgr.GRPMANAGERID='%s')",$usrid))
                .notExists(String.format("select 1 from msg_annouces_read anr where anr.ANNOUCEID=t.ANNOUCEID and anr.readusr='%s'",$usrid))
        );
        return new CmdResVo(num.intValue());
    }

    @Override
    public CmdResVo annouceReaded(String $usrid,Integer annouceid)
    {
        Long num=msgAnnoucesReadService.getBaseMapper().selectCount(
                new QueryWrapper<MsgAnnoucesRead>()
                        .eq("readusr",$usrid)
                        .eq("annouceid",annouceid)
        );
        int ret=0;
        if(num.intValue()<=0) {
            MsgAnnoucesRead msgAnnoucesRead=new MsgAnnoucesRead();
            msgAnnoucesRead.setAnnouceid(annouceid);
            msgAnnoucesRead.setReadusr($usrid);
            msgAnnoucesRead.setReaddt(new Date());
            ret=msgAnnoucesReadService.getBaseMapper().insert(msgAnnoucesRead);
        }

        return new CmdResVo(ret,annouceid.toString());
    }
}




